package ldh.im.fxgui.stage;

import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import ldh.im.fxbase.util.CloseFactory;
import ldh.im.fxbase.util.FxThreadUtil;
import ldh.im.fxbase.util.ResizeHelper;
import ldh.im.fxbase.util.StageUtil;
import ldh.im.fxgui.Main;
import ldh.im.fxgui.handle.WebRtcClient;
import ldh.im.fxgui.util.UiUtil;

import java.io.Closeable;
import java.io.IOException;
import java.util.Set;

public class BaseStage implements Closeable {

    protected Stage newStage = null;
    protected String name;
    protected MediaPlayer mediaPlayer;
    public static double VIDEO_WIDTH = 640;
    public static double VIDEO_HEIGHT = 480;

    public BaseStage(String name) {
        this.name = name;
        CloseFactory.getInstance().addCloseable(name, this);
    }

    protected Stage createStage(Scene scene) {
        Stage primaryStage = new Stage();
        primaryStage.setScene(scene);
        primaryStage.initStyle(StageStyle.TRANSPARENT);

        ResizeHelper.addResizeListener(primaryStage);
        Stage mainStage = StageUtil.getStage();
        double x = mainStage.getX() + mainStage.getWidth() + 2;
        double y = mainStage.getY() + (mainStage.getHeight() - scene.getHeight())/2;
        if (x >= Screen.getPrimary().getVisualBounds().getMaxX()- 50) {
            x = mainStage.getX() - scene.getWidth() - 2;
        }
        primaryStage.setX(x);
        primaryStage.setY(y);
        primaryStage.show();
        primaryStage.toFront();

        newStage = primaryStage;
        return primaryStage;
    }

    protected void loadStyleFiles(Scene scene) {
        scene.getStylesheets().add(this.getClass().getResource("/css/var.css").toExternalForm());
        scene.getStylesheets().add(this.getClass().getResource("/css/stage.css").toExternalForm());
        scene.getStylesheets().add(this.getClass().getResource("/css/main.css").toExternalForm());
        scene.getStylesheets().add(this.getClass().getResource("/css/scrollbar.css").toExternalForm());
        scene.getStylesheets().add(this.getClass().getResource("/css/tabpane.css").toExternalForm());
        scene.getStylesheets().add(this.getClass().getResource("/css/bootstrapfx.css").toExternalForm());
        scene.getStylesheets().add(this.getClass().getResource("/css/message-voice.css").toExternalForm());
    }

    @Override
    public void close() throws IOException {
        CloseFactory.getInstance().remove(name);
        if (newStage != null) {
            Platform.isFxApplicationThread();
            FxThreadUtil.runLater(()->{
                newStage.close();
            });
        }
    }

    protected double calcWidth(Set<String> userIds) {
        String loginUserId = UiUtil.getLoginImUser().getUserId();
        Long size = userIds.stream().filter(u->!u.equals(loginUserId)).count();
        if (size <= 1) {
            return 200;
        } else if (size >= 2) {
            return 350;
        }
        return 350;
    }

    protected double calcHeight(Set<String> userIds) {
        String loginUserId = UiUtil.getLoginImUser().getUserId();
        Long sizeLong = userIds.stream().filter(u->!u.equals(loginUserId)).count();
        int size = sizeLong.intValue();
        int n = size / 2;
        if (n * 2 < size) {
            n++;
        }
        if (n > 3) n = 3;
        double height = n * (WebRtcClient.videoHeight + 10) + 200;
        return height;
    }
}
